package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @ClassNameDishController
 * @Description TODO
 * @Author钰玟
 * @Date2025/2/13 下午4:20
 * @Version 1.0
 **/
@RestController
@Slf4j
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param dishDTO 菜品信息
     * @return 新增菜品结果
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品，菜品信息：{}",dishDTO);
        dishService.addDishWithFlavour(dishDTO);
        // 清除菜品缓存
        String key = "dish_"+dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO 分页查询条件
     * @return 菜品分页查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> pageQueryDish(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.pageQueryDish(dishPageQueryDTO);


        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids 菜品id列表
     * @return 删除菜品结果
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("删除菜品，ids：{}",ids);
        dishService.deleteDish(ids);
        // 清除菜品缓存
        //获取菜品缓存keys
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id 菜品id
     * @return 菜品信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品，id：{}",id);
        DishVO dishVO = dishService.getDishWithFlavourById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO 菜品信息
     * @return 修改菜品结果
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品，菜品信息：{}",dishDTO);
        dishService.updateWithFlavour(dishDTO);
//        清除菜品缓存
        cleanCache("dish_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result updateDishStatus(@RequestParam("id") Long id,@PathVariable Integer status){
        log.info("修改菜品状态，ids：{},status:{}",id,status);
        dishService.updateDishStatus(id,status);
        // 清除菜品缓存
        cleanCache("dish_*");

        return Result.success();
    }

    private void cleanCache(String key) {
        Set keys = redisTemplate.keys(key);
        if(keys.size() > 0){
            redisTemplate.delete(keys);
        }
    }

    /**
     * 根据分类Id查询菜品列表
     * @param categoryId
     * @return
     */
    //有关套餐 新增菜品 展示菜品
    @GetMapping("/list")
    @ApiOperation("查询菜品列表")
    public Result<List<DishVO>> list(Long categoryId){
        log.info("查询菜品列表,{}",categoryId);
        List<DishVO> dishVOList = dishService.getDishList(categoryId);
        return Result.success(dishVOList);
    }

}
